using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
/*
 * 
 * Autor      : Jose Jornando de Carvalho Junior    
 * Data       : 10/04/2010 07:06:47
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace SERIX
{
   public class CEscritoriosLocais
   {
       private string IDT_ESLOC ;
       private string IDT_UNI_REG ;       
       private string DSC_ESLOC ;

	   /// <sumary>
       /// Construtor da classe escritorios_locais
       /// <sumary>
       public CEscritoriosLocais()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Escritorios_locais
       /// <sumary>
       public CEscritoriosLocais( string IDT_ESLOC, string IDT_UNI_REG, string DSC_ESLOC )
       {
          this.IDT_ESLOC = IDT_ESLOC;
          this.IDT_UNI_REG = IDT_UNI_REG;          
          this.DSC_ESLOC = DSC_ESLOC;
       }
	   
	   #region Propriedades
     	/// <sumary>
        /// Identificador do escritorio local
        /// <sumary>
        public string Idt_esloc 
		{
		   get
		   {
		       return this.IDT_ESLOC;
		   }
		   set
		   {
		       this.IDT_ESLOC = value;
		   }
		}
     	/// <sumary>
        /// Identificador da unidade regional
        /// <sumary>
        public string Idt_uni_reg 
		{
		   get
		   {
		       return this.IDT_UNI_REG;
		   }
		   set
		   {
		       this.IDT_UNI_REG = value;
		   }
		}
     	
     	/// <sumary>
        /// Descricao do escritorio local
        /// <sumary>
        public string Dsc_esloc 
		{
		   get
		   {
		       return this.DSC_ESLOC;
		   }
		   set
		   {
		       this.DSC_ESLOC = value;
		   }
		}
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Escritorios_locais.
        /// </summary>
        public void Inserir(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SRX_ESCRITORIOS_LOCAIS
			                  ( IDT_ESLOC, IDT_UNI_REG, DSC_ESLOC )
						   values
						      ( :IDT_ESLOC, :IDT_UNI_REG, :DSC_ESLOC )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_ESLOC",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_UNI_REG",OracleType.Number)); 				
				command.Parameters.Add(new OracleParameter("DSC_ESLOC",OracleType.VarChar)); 
				
                command.Parameters["IDT_ESLOC"].Value = this.IDT_ESLOC;
                command.Parameters["IDT_UNI_REG"].Value = this.IDT_UNI_REG;               
                command.Parameters["DSC_ESLOC"].Value = this.DSC_ESLOC;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Escritorios_locais.
        /// </summary>
        public void Alterar(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SRX_ESCRITORIOS_LOCAIS set 
			                   IDT_UNI_REG = :IDT_UNI_REG, DSC_ESLOC = :DSC_ESLOC
						   where
						       IDT_ESLOC = :IDT_ESLOC ";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_ESLOC",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_UNI_REG",OracleType.Number)); 				
				command.Parameters.Add(new OracleParameter("DSC_ESLOC",OracleType.VarChar)); 
				
                command.Parameters["IDT_ESLOC"].Value = this.IDT_ESLOC;
                command.Parameters["IDT_UNI_REG"].Value = this.IDT_UNI_REG;                
                command.Parameters["DSC_ESLOC"].Value = this.DSC_ESLOC;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Escritorios_locais.
        /// </summary>
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SRX_ESCRITORIOS_LOCAIS", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("IDT_ESLOC", DbType.Int32, this.IDT_ESLOC));
            tab.delete();
        }
		
		static public DataTable ObterTodos(string login)
        {
            
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_ESCRITORIOS_LOCAIS order by DSC_ESLOC ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CEscritoriosLocais PesquisaporCodigo(string idt_esloc, string login)
        {
            CEscritoriosLocais obj;
            string SQL = @"SELECT *
                            FROM  Serix.SRX_ESCRITORIOS_LOCAIS 
                            WHERE IDT_ESLOC = :IDT_ESLOC";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":IDT_ESLOC", DbType.String, idt_esloc);
               
				qy.Executar();
                obj = new CEscritoriosLocais( qy.ObterCampo("IDT_ESLOC"), qy.ObterCampo("IDT_UNI_REG"), qy.ObterCampo("DSC_ESLOC") );
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CEscritoriosLocais();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt(string login)
        {
            string SQL = @"SELECT PKG_BIBSERIX.OBTER_IDT_ESLOC() IDT From dual ";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.Executar();
				return Convert.ToInt32(qy.ObterCampo("IDT"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return-1;
            }
			finally
            {
                qy.FecharConexao();
            }
        }

        static public DataTable ObterPorIDT_UNI_REG(string idt_uni_reg, string login)
        {
            
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_ESCRITORIOS_LOCAIS  where IDT_UNI_REG = :IDT_UNI_REG order by DSC_ESLOC ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_UNI_REG", OracleType.Number));
                command.Parameters["IDT_UNI_REG"].Value = idt_uni_reg;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }

        static public DataTable ObterDescricoes(string login)
        {
            string SQL = "select dsc_esloc from srx_escritorios_locais";
            Query qy = null;
            DataTable dt = null;

            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
                qy.Executar();
                dt = qy.ObterDataTable();
                return dt;
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return null;
            }
            finally
            {
                qy.FecharConexao();
            }
        }
    }
}	

